<?php

namespace App\Http\Controllers;

use App\Models\Expert;
use App\Models\User;
use Dotenv\Validator;
use http\Message;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\Auth;
use Illuminate\Support\Facades\Hash;
use voku\helper\ASCII;

class UserController extends Controller
{
    /**
     * Display a listing of the resource.
     *
     * @return \Illuminate\Http\Response
     */
    public function index()
    {
        $oUsers=User::paginate(5);
        return view('admin.user_list')
        ->with('oUsers',$oUsers);

    }

    /**
     * Show the form for creating a new resource.
     *
     * @return \Illuminate\Http\Response
     */
    public function create()
    {
        return view('admin.create_user');
    }

    /**
     * Store a newly created resource in storage.
     *
     * @param  \Illuminate\Http\Request  $request
     * @return \Illuminate\Http\Response
     */
    public function store(Request $request)
    {
        $user=$request->all();
        $rules=[
            'name'=>'required|min:2|unique:users',
            'email'=>'required|email|unique:users',
            'password'=>'required|min:6|confirmed'
        ];
        $message=[
            'name.required'=>'用户名不能为空',
            'name.min'=>'用户名至少有:min个字符',
            'name.unique'=>'用户名已存在，不能重复注册',
            'email.required'=>'邮箱不能为空',
            'email.email'=>'邮箱格式不正确',
            'email.unique'=>'邮箱已存在，不能重复注册',
            'password.required'=>'密码不能为空',
            'password.min'=>'密码长度不能少于:min个字符',
            'password.confirmed'=>'两次密码输入不一致',
        ];
        $validate=\Illuminate\Support\Facades\Validator::make($user,$rules,$message);//创建验证器
        if ($validate->fails()){
            $msg=$validate->messages()->first();
            return json_encode(['success'=>false,'msg'=>$msg]);
        }
        $user['password']=Hash::make($user['password']);
        $user['role'] = 'admin';
        User::create($user);
        return json_encode(['success'=>true,'msg'=>'添加成功']);
    }

    /**
     * Display the specified resource.
     *
     * @param  \App\Models\User  $user
     * @return \Illuminate\Http\Response
     */
    public function show(User $user)
    {
        //
    }

    /**
     * Show the form for editing the specified resource.
     *
     * @param  \App\Models\User  $user
     * @return \Illuminate\Http\Response
     */
    public function edit(User $user)
    {
        return view('admin.edit_user')
            ->with('oUser',$user);
    }

    /**
     * Update the specified resource in storage.
     *
     * @param  \Illuminate\Http\Request  $request
     * @param  \App\Models\User  $user
     * @return \Illuminate\Http\Response
     */
    public function update(Request $request, User $user)
    {
        $datas=$request->all();
        $rules=[
            'password'=>'required|min:6|confirmed',
        ];
        $message=[
            'password.required'=>'密码不能为空',
            'password.min'=>'密码长度不能少于:min个字符',
            'password.confirmed'=>'两次密码输入不一致',
        ];
        $validate=\Illuminate\Support\Facades\Validator::make($datas,$rules,$message);
        if ($validate->fails()){
            $msg=$validate->messages()->first();
            return json_encode(['success'=>false,'msg'=>$msg]);
        }
        $user['password']=Hash::make($datas['password']);
        if($user->role=='doctor'){
            $expert = Expert::where('email',$user->email)->get();
            $expert = Expert::find($expert[0]->id);
            $expert->password = Hash::make($request['password']);
            $expert->save();
        }
        $user->save();
        return json_encode(['success'=>true,'msg'=>'修改成功']);
    }

    /**
     * Remove the specified resource from storage.
     *
     * @param  \App\Models\User  $user
     * @return \Illuminate\Http\Response
     */
    public function destroy(User $user)
    {
        $user->delete();
        return json_encode(['msg'=>'删除成功']);
    }

    public function home(){
        return view('admin.index');
    }

    public function login(){
        return view('admin.login');
    }
    public function doLogin(Request $request){
        if(Auth::attempt(['email'=>$request->email,'password'=>$request->password])){
            return json_encode(['success'=>true,'msg'=>'登录成功']);
        }
        return json_encode(['success'=>false,'msg'=>'用户名或密码错误']);
    }
    //注销
    public function logout(){
        Auth::logout();
        return redirect('/login');
    }
}
